/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
package com.ruoyi.mall.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.mall.api.RemoteOrderInfoService;
import com.ruoyi.mall.api.RemoteOrderLogisticsService;
import com.ruoyi.mall.api.RemoteWxUserService;
import com.ruoyi.mall.api.constant.CommonConstants;
import com.ruoyi.mall.api.constant.MyReturnCode;
import com.ruoyi.mall.api.entity.OrderInfo;
import com.ruoyi.mall.api.entity.OrderLogistics;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * 商城订单
 *
 * @author JL
 * @date 2019-09-10 15:21:22
 */
@Slf4j
@RestController
@RequestMapping("/orderinfo")
@Api(value = "orderinfo", tags = "商城订单管理")
public class OrderInfoController extends BaseController {
    @DubboReference
    private RemoteOrderInfoService orderInfoService;
    @DubboReference
    private RemoteOrderLogisticsService orderLogisticsService;
    @DubboReference
    private RemoteWxUserService wxUserService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param orderInfo 商城订单
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    @SaCheckPermission("mall:orderinfo:index")
    public R getOrderInfoPage(Page page, OrderInfo orderInfo) {
        return R.ok(orderInfoService.page1(page, Wrappers.query(orderInfo)));
    }

    /**
     * 查询数量
     *
     * @param orderInfo
     * @return
     */
    @ApiOperation(value = "查询数量")
    @GetMapping("/count")
    public R getCount(OrderInfo orderInfo) {
        return R.ok(orderInfoService.count(orderInfo));
    }

    /**
     * 通过id查询商城订单
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id查询商城订单")
    @GetMapping("/{id}")
    @SaCheckPermission("mall:orderinfo:get")
    public R getById(@PathVariable("id") String id) {
        OrderInfo orderInfo = orderInfoService.getById(id);
        OrderLogistics orderLogistics = orderLogisticsService.getById(orderInfo.getLogisticsId());
        orderInfo.setOrderLogistics(orderLogistics);
        orderInfo.setUserInfo(wxUserService.getById(orderInfo.getUserId()));
        return R.ok(orderInfo);
    }

    /**
     * 新增商城订单
     *
     * @param orderInfo 商城订单
     * @return R
     */
    @ApiOperation(value = "新增商城订单")
    @PostMapping
    @SaCheckPermission("mall:orderinfo:add")
    public R save(@RequestBody OrderInfo orderInfo) {
        return R.ok(orderInfoService.save(orderInfo));
    }

    /**
     * 修改商城订单
     *
     * @param orderInfo 商城订单
     * @return R
     */
    @ApiOperation(value = "修改商城订单")
    @PutMapping
    @SaCheckPermission("mall:orderinfo:edit")
    public R updateById(@RequestBody OrderInfo orderInfo) {
        return R.ok(orderInfoService.updateById(orderInfo));
    }

    /**
     * 通过id删除商城订单
     *
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id删除商城订单")
    @DeleteMapping("/{id}")
    @SaCheckPermission("mall:orderinfo:del")
    public R removeById(@PathVariable String id) {
        return R.ok(orderInfoService.removeById(id));
    }

    /**
     * 取消商城订单
     *
     * @param id 商城订单
     * @return R
     */
    @ApiOperation(value = "取消商城订单")
    @PutMapping("/cancel/{id}")
    @SaCheckPermission("mall:orderinfo:edit")
    public R orderCancel(@PathVariable String id) {
        OrderInfo orderInfo = orderInfoService.getById(id);
        if (orderInfo == null) {
            return R.fail(MyReturnCode.ERR_70005.getCode(), MyReturnCode.ERR_70005.getMsg());
        }
        if (!CommonConstants.NO.equals(orderInfo.getIsPay())) {//只有未支付订单能取消
            return R.fail(MyReturnCode.ERR_70001.getCode(), MyReturnCode.ERR_70001.getMsg());
        }
        orderInfoService.orderCancel(orderInfo);
        return R.ok();
    }

}
